package operator.function;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Function;
import util.RxJavaHelper;

class OnErrorReturnOperator {

    public static void main(String[] args) {
        onErrorReturn();
    }

    /**
     * 当发生错误时，通过onErrorReturn正常结束
     */
    static void onErrorReturn(){
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onError(RxJavaHelper.getException());
            }
        })
                .onErrorReturn(new Function<Throwable, Integer>() {
                    @Override
                    public Integer apply(Throwable throwable) throws Exception {
                        return 3;
                    }
                })
                .subscribe(RxJavaHelper.getConsumer());
    }
}
